package com.rainxing.cockpit.modules.hotline.controller;


import com.rainxing.cockpit.modules.hotline.service.HotlineService;
import com.rainxing.cockpit.redis.RedisHandle;
import com.rainxing.cockpit.utils.DepartmentTools;
import com.rainxing.cockpit.utils.JsonResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yabin
 * @since 2018-07-26
 */
@Api(value = "热线接口管理", description = "热线接口管理")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Successful — 请求已完成"),
        @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
        @ApiResponse(code = 401, message = "未授权客户机访问数据"),
        @ApiResponse(code = 403, message = "拒绝或者禁止访问"),
        @ApiResponse(code = 404, message = "服务器找不到给定的资源；文档不存在"),
        @ApiResponse(code = 500, message = "服务器不能完成请求")}
)
@RestController
@Slf4j
@RequestMapping("/hotline")
public class HotlineController {
    //注入 redis工具类
    @Autowired
    private RedisHandle redisHandle;
    //注入热线接口
    @Autowired
    private HotlineService hotlineService;

    @ApiOperation(value = "热线案由Top10", notes = "根据url的参数来获取热线案由Top10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("hotLineTop10")
    private ResponseEntity<JsonResult> getHotLineTop10ByTime(String startTime, String endTime) {
        JsonResult r = new JsonResult();
        Map<String, Object> map = new LinkedHashMap<>();
        Object top10Map = redisHandle.get("hr:" + "top10Map");
        if (top10Map != null) {
            map = (Map<String, Object>) top10Map;
        }
        if (map.isEmpty()) {
            try {
                map = hotlineService.hotLineTop10(startTime, endTime);
                r.setResult(map);
                r.setStatus("ok");
            } catch (Exception e) {
                r.setResult(e.getClass().getName() + ":" + e.getMessage());
                r.setStatus("error");
                e.printStackTrace();
            }
        } else {
            map = (Map<String, Object>) redisHandle.get("hr:" + "top10Map");
            r.setResult(map);
            r.setStatus("ok");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "首页 热线举报量排名前三", notes = "根据url的参数来获取首页 热线举报量排名前三")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("reportTop3")
    private ResponseEntity<JsonResult> getReportTop3(String startTime, String endTime) {
        JsonResult r = new JsonResult();
        Map<String, Object> map = new LinkedHashMap<>();
        Object top3Map = redisHandle.get("hr:" + "top3Map");
        if (top3Map != null) {
            map = (Map<String, Object>) top3Map;
        }
        if (map.isEmpty()) {
            try {
                top3Map = hotlineService.getReportTop3(startTime, endTime);
                r.setResult(top3Map);
                r.setStatus("ok");
            } catch (Exception e) {
                r.setResult(e.getClass().getName() + ":" + e.getMessage());
                r.setStatus("error");
                e.printStackTrace();
            }
        } else {
            map = (Map<String, Object>) redisHandle.get("hr:" + "top3Map");
            r.setResult(map);
            r.setStatus("ok");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "热线电话受理来源", notes = "根据url的参数来获取热线电话受理来源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("sourceCount")
    private ResponseEntity<JsonResult> getSourceCount(String startTime, String endTime) {
        JsonResult r = new JsonResult();
        Map<String, Integer> map = new HashMap<>();
        Object hotLineSourceCount = redisHandle.get("hr:" + "hotLineSourceCount");
        if (hotLineSourceCount != null) {
            map = (Map<String, Integer>) hotLineSourceCount;
        }
        if (map.isEmpty()) {
            try {
                map = hotlineService.sourceCount(startTime, endTime);
                r.setResult(map);
                r.setStatus("ok");
            } catch (Exception e) {
                r.setResult(e.getClass().getName() + ":" + e.getMessage());
                r.setStatus("error");
                e.printStackTrace();
            }
        } else {
            map = (Map<String, Integer>) redisHandle.get("hr:" + "hotLineSourceCount");
            r.setResult(map);
            r.setStatus("ok");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "首页 各月份中各执法队举报量排名", notes = "根据url的参数来获取首页 各月份中各执法队举报量排名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("reportMonthList")
    private ResponseEntity<JsonResult> getReportMonthList(String startTime, String endTime) {
        JsonResult r = new JsonResult();
        List<Map<String, String>> list = null;
        String key = "hr:";
        list = (List<Map<String, String>>) redisHandle.get(key + "reportMonthList");
        if (list == null) {
            try {
                //首页 各月份中各执法队举报量排名
                list = hotlineService.getReportMonthList(startTime, endTime);
                r.setResult(DepartmentTools.oldToNewName(list));
                r.setStatus("ok");
            } catch (Exception e) {
                r.setResult(e.getClass().getName() + ":" + e.getMessage());
                r.setStatus("error");
                e.printStackTrace();
            }
        } else {
            //热线举报量
            list = (List<Map<String, String>>) redisHandle.get(key + "reportMonthList");
            r.setResult(DepartmentTools.oldToNewName(list));
            r.setStatus("ok");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "街道下各执法队排名", notes = "根据url的参数来获取街道下各执法队排名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("reportDepartmentList")
    private ResponseEntity<JsonResult> getReportDepartmentList(String startTime, String endTime) {
        JsonResult r = new JsonResult();
        Map<String, Object> map = new LinkedHashMap<>();
        List<Map<String, String>> list = null;
        String key = "hr:";
        Object rankMap = redisHandle.get(key + "reportDepartmentMap");
        if (rankMap != null) {
            map = (Map<String, Object>) rankMap;
        }
        if (map.isEmpty()) {
            try {
                //街道下各执法队排名
                map = hotlineService.getReportDepartmentList(startTime, endTime);
                list = (List<Map<String, String>>) map.get("rankList");
                map.put("rankList", DepartmentTools.oldToNewName(list));
                r.setResult(map);
                r.setStatus("ok");
            } catch (Exception e) {
                r.setResult(e.getClass().getName() + ":" + e.getMessage());
                r.setStatus("error");
                e.printStackTrace();
            }
        } else {
            //热线举报量
            map = (Map<String, Object>) redisHandle.get(key + "reportDepartmentMap");
            list = (List<Map<String, String>>) map.get("rankList");
            map.put("rankList", DepartmentTools.oldToNewName(list));
            r.setResult(map);
            r.setStatus("ok");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "街道下各执法队详情", notes = "根据url的参数来获取街道下各执法队详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("reportDepartmentInfo")
    private ResponseEntity<JsonResult> getReportDepartmentInfo(String startTime, String endTime) {
        JsonResult r = new JsonResult();
        Map<String, Object> map = new LinkedHashMap<>();
        List<Map<String, String>> list = null;
        String key = "hr:";
        Object rankMap = redisHandle.get(key + "reportInfoMap");
        if (rankMap != null) {
            map = (Map<String, Object>) rankMap;
        }
        if (map.isEmpty()) {
            try {
                //街道下各执法队详情
                map = hotlineService.getReportDepartmentInfo(startTime, endTime);
                list = (List<Map<String, String>>) map.get("rankList");
                map.put("rankList", DepartmentTools.oldToNewName(list));
                r.setResult(map);
                r.setStatus("ok");
            } catch (Exception e) {
                r.setResult(e.getClass().getName() + ":" + e.getMessage());
                r.setStatus("error");
                e.printStackTrace();
            }
        } else {
            //热线举报量
            map = (Map<String, Object>) redisHandle.get(key + "reportInfoMap");
            list = (List<Map<String, String>>) map.get("rankList");
            map.put("rankList", DepartmentTools.oldToNewName(list));
            r.setResult(map);
            r.setStatus("ok");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "首页举报量上升、举报量下降", notes = "根据url的参数来获取首页举报量上升、举报量下降")
    @GetMapping("reportRank")
    private ResponseEntity<JsonResult> getReportRankList() {
        JsonResult r = new JsonResult();
        List<Map<String, String>> list = null;
        String key = "hr:";
        Map<String, Object> map = new LinkedHashMap<>();
        Object reportRankMap = redisHandle.get(key + "reportRank");
        if (reportRankMap != null) {
            map = (Map<String, Object>) reportRankMap;
        }
        if (map.isEmpty()) {
            try {
                //首页举报量上升、举报量下降
                map = hotlineService.getReportRankList();
                r.setResult(map);
//                r.setResult(DepartmentTools.oldToNewName(list));
                r.setStatus("ok");
            } catch (Exception e) {
                r.setResult(e.getClass().getName() + ":" + e.getMessage());
                r.setStatus("error");
                e.printStackTrace();
            }
        } else {
            //热线举报量
            map = (Map<String, Object>) redisHandle.get(key + "reportRank");
            r.setResult(map);
//            r.setResult(DepartmentTools.oldToNewName(list));
            r.setStatus("ok");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "首页 各个统计数、百分比", notes = "根据url的参数来获取首页 各个统计数、百分比")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("indexCount")
    private ResponseEntity<JsonResult> getIndexCount(String startTime, String endTime) {
        JsonResult r = new JsonResult();
        Map<String, Object> map = new LinkedHashMap<>();
        Object indexCountMap = redisHandle.get("hr:" + "indexCountMap");
        if (indexCountMap != null) {
            map = (Map<String, Object>) indexCountMap;
        }
        if (map.isEmpty()) {
            try {
                map = hotlineService.getIndexCount(startTime, endTime);
                r.setResult(map);
                r.setStatus("ok");
            } catch (Exception e) {
                r.setResult(e.getClass().getName() + ":" + e.getMessage());
                r.setStatus("error");
                e.printStackTrace();
            }
        } else {
            map = (Map<String, Object>) redisHandle.get("hr:" + "indexCountMap");
            r.setResult(map);
            r.setStatus("ok");
        }
        return ResponseEntity.ok(r);
    }
}

